package org.zjx.analysis.analyze;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.HashMap;
import java.util.Map;

/**
 * 基础图片分析器
 */
public abstract class AbstractImageAnalyzer implements ImageAnalyzer {
    public Map<String, ResolutionArea> areaMap = new HashMap<>();
    public Map<String, ResolutionImage> imageMap = new HashMap<>();

    protected final String analyzerName;
    protected final String sceneType;

    public AbstractImageAnalyzer(String analyzerName, String sceneType) {
        this.analyzerName = analyzerName;
        this.sceneType = sceneType;
        initResources();
    }

    @Override
    public boolean matches(BufferedImage image) {
        if (image == null) {
            return false;
        }

        try {
            // 轻量级检查，只判断是否匹配
            return isSceneMatch(image);
        } catch (Exception e) {
            System.err.println("匹配检查异常 [" + analyzerName + "]: " + e.getMessage());
            return false;
        }
    }


    protected abstract void initResources();

    @Override
    public String getAnalyzerName() {
        return analyzerName;
    }

    @Override
    public String getSceneType() {
        return sceneType;
    }

    /**
     * 轻量级场景匹配检查
     */
    protected abstract boolean isSceneMatch(BufferedImage image);


    @Data
    @AllArgsConstructor
    public static class ResolutionArea {
        private Rectangle rectangle;
    }

    @Data
    @AllArgsConstructor
    public static class ResolutionImage {
        private Rectangle rectangle;
        private BufferedImage pattern;
    }
}